package com.hub.realtime.resource.service.impl;

import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hub.realtime.common.core.domain.model.LoginUser;
import com.hub.realtime.common.exception.UtilException;
import com.hub.realtime.common.utils.StringUtils;
import com.hub.realtime.framework.service.ServiceBase;
import com.hub.realtime.resource.domain.ResApplication;
import com.hub.realtime.resource.dto.input.CatalogQueryParam;
import com.hub.realtime.resource.service.IResApplicationService;
import org.apache.flink.table.catalog.hive.HiveCatalog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hub.realtime.resource.mapper.ResCatalogMapper;
import com.hub.realtime.resource.domain.ResCatalog;
import com.hub.realtime.resource.service.IResCatalogService;

/**
 * 元数据中心Service业务层处理
 *
 * @author wupeng
 * @date 2022-02-09
 */
@Service
public class ResCatalogServiceImpl extends ServiceBase<ResCatalogMapper, ResCatalog> implements IResCatalogService {


    @Autowired
    IResApplicationService applicationService;

    @Override
    public Page<ResCatalog> list(CatalogQueryParam param) {
        LambdaQueryWrapper<ResCatalog> wrapper = createCommonWrapper(ResCatalog::getUserId, ResCatalog::getCreateTime);
        if (StringUtils.isNotEmpty(param.getCatalogName())) {
            wrapper.like(ResCatalog::getCatalogName, param.getCatalogName());
        }
        Page<ResCatalog> pageList = getPageList(param, wrapper);
        if (!pageList.getRecords().isEmpty()) {
            pageList.getRecords().forEach(l -> {
                Long appId = l.getAppId();
                ResApplication application = applicationService.getBaseMapper().selectById(appId);
                if(application!=null)
                {
                    l.setAppName(application.getName());
                }
            });
        }
        return pageList;
    }

    @Override
    public int deleteInfos(Long[] ids) {
        return delByIds(ids);
    }

    @Override
    public void testCatalog(ResCatalog resCatalog) {
        try {
            HiveCatalog hiveCatalog = new HiveCatalog(resCatalog.getCatalogName(), null, resCatalog.getHiveConfDir());
            hiveCatalog.open();
            List<String> list = hiveCatalog.listDatabases();
            hiveCatalog.close();
        } catch (Exception ex) {
            throw new UtilException(ex);
        }
    }

    @Override
    public int addCatalog(ResCatalog resCatalog) {
        testCatalog(resCatalog);
        LoginUser loginUser = getLoginUser();
        resCatalog.setCreateBy(loginUser.getUsername());
        resCatalog.setCreateTime(DateUtil.date());
        resCatalog.setUserId(loginUser.getUserId());
        try {
            return baseMapper.insert(resCatalog);
        } catch (Exception ex) {
            throw new UtilException(ex);
        }

    }

    @Override
    public int updateCatalog(ResCatalog resCatalog) {
        testCatalog(resCatalog);
        LoginUser loginUser = getLoginUser();
        resCatalog.setUpdateTime(DateUtil.date());
        resCatalog.setUpdateBy(loginUser.getUsername());
        try {
            return baseMapper.updateById(resCatalog);
        } catch (Exception ex) {
            throw new UtilException(ex);
        }
    }

    @Override
    public List<ResCatalog> getCatalogList() {
        LambdaQueryWrapper<ResCatalog> wrapper = createCommonWrapper(ResCatalog::getUserId, ResCatalog::getCreateTime);
        return baseMapper.selectList(wrapper);
    }
}

